<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-page-header class="mb-4" content="价格管理" @back="$router.back()" />
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="出行日期" prop="travel_time">
                    <daterange-picker
                        v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time"
                    />
                </el-form-item>

                <el-form-item label="状态" class="w-[280px]" prop="status">
                    <el-select  v-model="queryParams.status" clearable placeholder="请选择状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="(item, index) in dictData.approval_status"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mb-4" shadow="never">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item>{{ lineData.lineDetail.travel_agency_name }}</el-breadcrumb-item>
            <el-breadcrumb-item>{{ lineData.lineDetail.title }}</el-breadcrumb-item>
          </el-breadcrumb>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['travel.line_price/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button
                v-perms="['travel.line_price/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData)"
            >
                删除
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="线路" prop="line_name">
                    </el-table-column>
                    <el-table-column label="价格" prop="price" show-overflow-tooltip />
                    <el-table-column label="出行日期" prop="travel_time">
                        <template #default="{ row }">
                          <span>{{ row.travel_time ? timeFormat(row.travel_time, 'yyyy-mm-dd hh:MM:ss') : '' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status">
                      <template #default="{ row }">
                        <el-tag class="ml-[-10px]" :type="row.status_remark">{{
                            row.status_value
                          }}</el-tag>
                      </template>
                    </el-table-column>
                    <el-table-column label="用户" prop="user_account" show-overflow-tooltip />

                    <el-table-column label="操作" width="150" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['travel.line_price/update_status']"
                                type="primary"
                                link
                                @click="handlePriceApprove(row)"
                            >
                              审批
                            </el-button>
                             <el-button
                                v-perms="['travel.line_price/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['travel.line_price/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
    <approve-popup
        v-if="showApprove"
        ref="approveRef"
        :id="approveId"
        @success="getLists"
        @close="showApprove = false"
    />
</template>

<script lang="ts" setup name="linePriceLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiLineDetail } from '@/api/line'
import { apiLinePriceLists, apiLinePriceDelete } from '@/api/line_price'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import ApprovePopup from "@/views/travel_agency/line_price/components/ApprovePopup.vue";
const { query } = useRoute()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const approveRef = shallowRef<InstanceType<typeof ApprovePopup>>()
// 是否显示编辑框
const showEdit = ref(false)

// 是否显示审批框
const showApprove = ref(false)
// 审批的线路ID
const approveId = ref<number>(0)
// 查询条件
const queryParams = reactive({
    line_id: Number(query.id),
    status: '',
    start_time: '',
    end_time: '',
})


// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('approval_status')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiLinePriceLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.setFormData({
        line_id: queryParams.line_id
    })
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiLinePriceDelete({ id })
    getLists()
}

// 审批
const handlePriceApprove = async (row: any) => {
  showApprove.value = true
  await nextTick()
  approveRef.value?.open(row.id)
}

const lineData = reactive<{
  lineDetail: any[]
}>({
  lineDetail: []
})

const getLineDetail = async () => {
  lineData.lineDetail = await apiLineDetail({ id: queryParams.line_id })
}
getLists()
getLineDetail()
</script>

